using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using Kotonoha.Server.Objects.Model;


namespace Kotonoha.Objects.Selectors {
    public class SimpleWordSelector : AbstractOneTimedSelector {
        public SimpleWordSelector(Container container) : base(container) {}

        private static readonly Random rand = new Random();

        protected override Word resolve() {
            List<Word> words = container.Words;
            List<double> positions = new List<double>();
            double sum = 0.0;
            foreach (var word in words) {
                double val = RepVal(word);
                positions.Add(val);
                sum += val;
            }

            double selected = rand.NextDouble() * sum;

            sum = 0;
            for (int i = 0; i < positions.Count; ++i ) {
                sum += positions[i];
                if (sum > selected) {
                    return words[i];
                }
            }
            throw new Exception("Should never happen, lol");
        }

        private static double RepVal(Word word) {
            return 1/word.Repetition.Value;
        }
    }
}